package com.htgd.gardenexpo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeConIntegralCategoryDao;
import com.htgd.gardenexpo.entity.WxUserCoupon;
import com.htgd.gardenexpo.dao.WxUserCouponDao;
import com.htgd.gardenexpo.page.PageBase;
import com.htgd.gardenexpo.service.WxUserCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.CouponCenterVO;
import com.htgd.gardenexpo.vo.MyCouponRecordVO;
import com.htgd.gardenexpo.vo.MyCouponVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 优惠券和用户关系 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-08
 */
@Service
public class WxUserCouponServiceImpl extends ServiceImpl<WxUserCouponDao, WxUserCoupon> implements WxUserCouponService {

    @Autowired
    private WxUserCouponDao wxUserCouponDao;

    @Autowired
    private GeConIntegralCategoryDao geConIntegralCategoryDao;
    @Override
    public PageInfo<MyCouponVO> myCoupon(String openId, PageBase pageBase) {
//        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
//        List<MyCouponVO> result =new ArrayList<>();

        PageInfo<MyCouponVO> result = new PageInfo<>();

          List<MyCouponVO> myCouponVOS = wxUserCouponDao.myCoupon(openId);
        Iterator<MyCouponVO> iterator = myCouponVOS.iterator();

        while (iterator.hasNext()) {
            MyCouponVO myCouponVO = iterator.next();
            //使用时间
            if(myCouponVO.getValidityType()==1){
                myCouponVO.setTimeText("长期有效");
            }else if(myCouponVO.getValidityType()==2){
                LocalDateTime localDateTime = myCouponVO.getCreateTime().plusDays(myCouponVO.getFixedDays()).withHour(0).withMinute(0).withSecond(0).withNano(0);
                if(LocalDateTime.now().isBefore(localDateTime)){
//                if(LocalDateTime.of(2024, 8, 19, 10, 6, 57).isBefore(localDateTime)){
                    long days = myCouponVO.getFixedDays()-1;
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    String start = myCouponVO.getCreateTime().format(formatter);
                    String end =myCouponVO.getCreateTime().plusDays(days).format(formatter);
                    myCouponVO.setTimeText(start+" 至 "+end);
                }else {
                    iterator.remove(); // 使用 Iterator 的 remove 方法安全地移除元素
                }
            }else {
                if(LocalDate.now().isBefore(myCouponVO.getFixedTime().plusDays(1))){
//                if(LocalDate.of(2024,8,17).isBefore(myCouponVO.getFixedTime().plusDays(1))){
                    myCouponVO.setTimeText(myCouponVO.getFixedTime().plusDays(1)+"之前有效");
                }else {
                    iterator.remove(); // 使用 Iterator 的 remove 方法安全地移除元素
                }
            }

            //使用条件
            if(myCouponVO.getConditionType()==0){
                myCouponVO.setConditionText("无条件");
            }else {
                myCouponVO.setConditionText("满"+myCouponVO.getReductionAmount()+"元可用");
            }

            //类目
            String categoryName = geConIntegralCategoryDao.getNameById(myCouponVO.getCategoryId());
            myCouponVO.setCategoryName(categoryName);
        }
        int num = myCouponVOS.size();

        // 页码从1开始，所以要减1
        int fromIndex = (pageBase.getPageNum() - 1) * pageBase.getPageSize();
        int toIndex = Math.min(fromIndex + pageBase.getPageSize(), myCouponVOS.size());

        if (fromIndex >= myCouponVOS.size()) {
            result.setTotal(num);
            result.setList(new ArrayList<>());
        }else {
            result.setTotal(num);
            result.setList(myCouponVOS.subList(fromIndex, toIndex));
        }

        return result;

    }

    @Override
    public PageInfo<MyCouponRecordVO> myCouponRecord(String openId, PageBase pageBase) {
        PageInfo<MyCouponRecordVO> result = new PageInfo<>();

        List<MyCouponRecordVO> myCouponRecordVOS = wxUserCouponDao.myCouponRecord(openId);

        List<MyCouponRecordVO> myCouponRecordVOS2 = wxUserCouponDao.myCouponRecord2(openId);
        Iterator<MyCouponRecordVO> iterator = myCouponRecordVOS2.iterator();
        while (iterator.hasNext()) {
            MyCouponRecordVO myCouponRecordVO = iterator.next();
            //使用时间
            if(myCouponRecordVO.getValidityType()==1){
                continue;
            }else if(myCouponRecordVO.getValidityType()==2){
                LocalDateTime localDateTime = myCouponRecordVO.getCreateTime().plusDays(myCouponRecordVO.getFixedDays()).withHour(0).withMinute(0).withSecond(0).withNano(0);
                if(LocalDateTime.now().isBefore(localDateTime)){//未过期
//                if(LocalDateTime.of(2024, 8, 19, 10, 6, 57).isBefore(localDateTime)){
                    continue;
                }
            }else {
                if(LocalDate.now().isBefore(myCouponRecordVO.getFixedTime().plusDays(1))){
//                if(LocalDate.of(2024,8,17).isBefore(myCouponRecordVO.getFixedTime().plusDays(1))){
                    continue;
                }
            }


            myCouponRecordVO.setCouponStatus("2");

            myCouponRecordVOS.add(myCouponRecordVO);
        }

        for (MyCouponRecordVO myCouponRecordVO : myCouponRecordVOS) {
            //使用条件
            if(myCouponRecordVO.getConditionType()==0){
                myCouponRecordVO.setConditionText("无条件");
            }else {
                myCouponRecordVO.setConditionText("满"+myCouponRecordVO.getReductionAmount()+"元可用");
            }

            //使用时间
            if(myCouponRecordVO.getValidityType()==1){
                myCouponRecordVO.setTimeText("长期有效");
            }else if(myCouponRecordVO.getValidityType()==2){
                    long days = myCouponRecordVO.getFixedDays()-1;
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    String start = myCouponRecordVO.getCreateTime().format(formatter);
                    String end =myCouponRecordVO.getCreateTime().plusDays(days).format(formatter);
                    myCouponRecordVO.setTimeText(start+" 至 "+end);
            }else {
                    myCouponRecordVO.setTimeText(myCouponRecordVO.getFixedTime().plusDays(1)+"之前有效");
            }

            //类目
            String categoryName = geConIntegralCategoryDao.getNameById(myCouponRecordVO.getCategoryId());
            myCouponRecordVO.setCategoryName(categoryName);
        }

        int num = myCouponRecordVOS.size();

        // 页码从1开始，所以要减1
        int fromIndex = (pageBase.getPageNum() - 1) * pageBase.getPageSize();
        int toIndex = Math.min(fromIndex + pageBase.getPageSize(), myCouponRecordVOS.size());

        if (fromIndex >= myCouponRecordVOS.size()) {
            result.setTotal(num);
            result.setList(new ArrayList<>());
        }else {
            result.setTotal(num);
            result.setList(myCouponRecordVOS.subList(fromIndex, toIndex));
        }

        return result;
    }

    @Override
    public Integer getCouponNum(String openId) {
        List<MyCouponVO> myCouponVOS = wxUserCouponDao.myCoupon(openId);
        Iterator<MyCouponVO> iterator = myCouponVOS.iterator();

        while (iterator.hasNext()) {
            MyCouponVO myCouponVO = iterator.next();
            //使用时间
            if(myCouponVO.getValidityType()==1){
                myCouponVO.setTimeText("长期有效");
            }else if(myCouponVO.getValidityType()==2){
                LocalDateTime localDateTime = myCouponVO.getCreateTime().plusDays(myCouponVO.getFixedDays()).withHour(0).withMinute(0).withSecond(0).withNano(0);
                if(LocalDateTime.now().isBefore(localDateTime)){
//                if(LocalDateTime.of(2024, 8, 19, 10, 6, 57).isBefore(localDateTime)){
                    long days = myCouponVO.getFixedDays()-1;
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    String start = myCouponVO.getCreateTime().format(formatter);
                    String end =myCouponVO.getCreateTime().plusDays(days).format(formatter);
                    myCouponVO.setTimeText(start+" 至 "+end);
                }else {
                    iterator.remove(); // 使用 Iterator 的 remove 方法安全地移除元素
                }
            }else {
                if(LocalDate.now().isBefore(myCouponVO.getFixedTime().plusDays(1))){
//                if(LocalDate.of(2024,8,17).isBefore(myCouponVO.getFixedTime().plusDays(1))){
                    myCouponVO.setTimeText(myCouponVO.getFixedTime().plusDays(1)+"之前有效");
                }else {
                    iterator.remove(); // 使用 Iterator 的 remove 方法安全地移除元素
                }
            }

            //使用条件
            if(myCouponVO.getConditionType()==0){
                myCouponVO.setConditionText("无条件");
            }else {
                myCouponVO.setConditionText("满"+myCouponVO.getReductionAmount()+"元可用");
            }

            //类目
            String categoryName = geConIntegralCategoryDao.getNameById(myCouponVO.getCategoryId());
            myCouponVO.setCategoryName(categoryName);
        }
        int num = myCouponVOS.size();
        return num;
    }
}
